package cn.wolfcode.wolf2w.strategy.service.impl;

import cn.wolfcode.wolf2w.strategy.api.domain.Strategy;
import cn.wolfcode.wolf2w.strategy.api.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.strategy.mapper.StrategyCatalogMapper;
import cn.wolfcode.wolf2w.strategy.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.strategy.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.strategy.service.IStrategyService;
import cn.wolfcode.wolf2w.strategy.vo.CatalogVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * 攻略分类Service业务层处理
 * 
 * @author dafei
 * @date 2023-11-22
 */
@Service
@Transactional
public class StrategyCatalogServiceImpl extends ServiceImpl<StrategyCatalogMapper,StrategyCatalog> implements IStrategyCatalogService {
    @Autowired
    @Lazy
    private IStrategyService strategyService;

    @Override
    public IPage<StrategyCatalog> queryPage(StrategyCatalogQuery qo) {
        IPage<StrategyCatalog> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .page(page);
    }

    @Override
    public List<CatalogVO> groupList() {

        List<CatalogVO> vos = new ArrayList<>();

//        select
//                dest_name,
//        GROUP_CONCAT(id) ids,
//                GROUP_CONCAT(name) names
//        from ta_strategy_catalog
//        GROUP BY dest_name

        QueryWrapper<StrategyCatalog> wrapper = new QueryWrapper<>();
        wrapper.select("dest_name,GROUP_CONCAT(id) ids,GROUP_CONCAT(name) names");
        wrapper.groupBy("dest_name");
        List<Map<String, Object>> maps = super.listMaps(wrapper);
        for (Map<String, Object> map : maps) {
            CatalogVO vo = new CatalogVO();

            String destName = map.get("dest_name").toString();
            String ids = map.get("ids").toString();
            String names = map.get("names").toString();
            List<StrategyCatalog> catalogList = this.parseCatalogList(ids, names);

            vo.setDestName(destName);
            vo.setCatalogList(catalogList);
            vos.add(vo);
        }
        return vos;
    }
    //解析攻略分类集合
    private List<StrategyCatalog> parseCatalogList(String ids, String names) {
        List<StrategyCatalog> list = new ArrayList<>();
        String[] idsA = ids.split(",");
        String[] namesA = names.split(",");
        for (int i = 0; i < idsA.length; i++) {
            StrategyCatalog sc = new StrategyCatalog();
            sc.setId(Long.parseLong(idsA[i]));
            sc.setName(namesA[i]);
            list.add(sc);
        }
        return list;
    }

    public List<CatalogVO> groupList2() {
        List<CatalogVO> vos = new ArrayList<>();
        //查询所有数据
        List<StrategyCatalog> list = lambdaQuery().orderByAsc(StrategyCatalog::getDestName).list();
        //java 封装
        String destName = list.get(0).getDestName();
        List<StrategyCatalog> scs = new ArrayList<>();
        for (StrategyCatalog sc : list) {
            if(destName.equals(sc.getDestName())){
                scs.add(sc);
            }else{
                CatalogVO vo = new CatalogVO();
                vo.setDestName(destName);
                vo.setCatalogList(scs);
                vos.add(vo);

                destName = sc.getDestName();
                scs = new ArrayList<>();
                scs.add(sc);
            }
        }
        return vos;
    }

    public List<CatalogVO> groupList3() {
        List<StrategyCatalog> list = lambdaQuery().orderByAsc(StrategyCatalog::getDestName).list();
        List<CatalogVO> vos = new ArrayList<>();
        Map<String, List<StrategyCatalog>> catalogMap = list.stream().collect(Collectors.groupingBy(StrategyCatalog::getDestName));
        catalogMap.forEach((destName, scs) -> {
            CatalogVO vo = new CatalogVO();
            vo.setDestName(destName);
            vo.setCatalogList(scs);
            vos.add(vo);
        });

        return vos;
    }

    @Override
    public List<StrategyCatalog> queryCatalogList(Long destId) {
        List<StrategyCatalog> list = lambdaQuery().eq(StrategyCatalog::getDestId, destId).list();

        for (StrategyCatalog sc : list) {
            List<Strategy> sts = strategyService.queryByCatalogId(sc.getId());
            //List<Strategy> sts = strategyService.list(new LambdaQueryWrapper<Strategy>().eq(Strategy::getCatalogId, sc.getId()));
            sc.setStrategies(sts);
        }
        return list;
    }
}
